﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Web;
using System.Web.Caching;

namespace GDBLL.Handler
{
    public class AjaxHandlerFactory : IHttpHandlerFactory
    {
        #region IHttpHandlerFactory Members

        /// <summary>
        /// Enables a factory to reuse an existing handler instance.
        /// </summary>
        /// <param name="handler">The <see cref="T:System.Web.IHttpHandler"></see> object to reuse.</param>
        public void ReleaseHandler(IHttpHandler handler)
        {
            // TODO:  Add AjaxHandlerFactory.ReleaseHandler implementation
        }

        /// <summary>
        /// Returns an instance of a class that implements the <see cref="T:System.Web.IHttpHandler"></see> interface.
        /// </summary>
        /// <param name="context">An instance of the <see cref="T:System.Web.HttpContext"></see> class that provides references to intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests.</param>
        /// <param name="requestType">The HTTP data transfer method (GET or POST) that the client uses.</param>
        /// <param name="url">The <see cref="P:System.Web.HttpRequest.RawUrl"></see> of the requested resource.</param>
        /// <param name="pathTranslated">The <see cref="P:System.Web.HttpRequest.PhysicalApplicationPath"></see> to the requested resource.</param>
        /// <returns>
        /// A new <see cref="T:System.Web.IHttpHandler"></see> object that processes the request.
        /// </returns>
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            /*
                时间检查,控制单用户的请求间隔
             */
            /*
                安全检查
             */
            /*
                AVL树进行搜索优化
             */
            string filename = Path.GetFileNameWithoutExtension(context.Request.Path);
            string privateToken = context.Request.QueryString["user"];

            switch (requestType)
            {
                case "GET":		// get the JavaScript files

                    switch (filename.ToLower())
                    {
                        case "Battle":
                            {
                                //如果在状态为战斗,检查标志位
                                if (1 == 1)
                                {
                                    //如果个人练级中
                                    if (1 == 1)
                                    {
                                        //练级模块
                                        return new BattleHandler(privateToken);
                                    }
                                    //如果PK中
                                    else if (1 == 1)
                                    {
                                        //个人战斗模块
                                    }
                                    else if (1 == 1)
                                    {
                                        //如果在队伍PK中
                                        if (1 == 1)
                                        {
                                            //取队伍战斗模块
                                        }



                                    }
                                }
                                break;
                            }
                        case "map":
                            return new MapHandler("map");
                    }
                    break;
                case "POST":
                    break;

            }

            //如果登录
            if (1 == 1)
            {
                
            }
            return new EmbeddedJavaScriptHandler("prototype");
        }

        #endregion
    }
}
